package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-10-28
 * Time: 13:36
 */
//通过这个类封装对blog表的增删改查
public class BlogDao {
    //1.新增一个博客,构造一个insert方法
    public void insert(Blog blog){
        Connection connection=null;
        PreparedStatement statement=null;

        try {
            //和数据库建立连接
            connection =DBUtil.getConnection();
            //构造SQL语句
            String sql="insert into blog values(null,?,?,?,now())";
            //准备好sql语句
            statement=connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3,blog.getUserId());
            //执行SQL语句
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭连接，释放资源
            DBUtil.close(connection,statement,null);
        }
    }

    //2.查询blog表中的所有博客
    public List<Blog> getblogs(){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<Blog> blogs=new ArrayList<>();//此时list为空

        try {
            //和数据库建立连接
            connection=DBUtil.getConnection();
            //构造sql语句
            String sql="select * from blog order by postTime desc";//按时间顺序降序排序
            //准备语句
            statement=connection.prepareStatement(sql);
            //执行sql
            resultSet=statement.executeQuery();
            //遍历结果集合
            while(resultSet.next()){
                Blog blog=new Blog();
                //前面是java类中的属性，后面则是从数据库的表中对应属性的值
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogs.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return blogs;
    }

    //指定ID，查询某一个博客
    public Blog getBlog(int blogId){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;

        try {
            connection=DBUtil.getConnection();
            String sql="select * from blog where blogId = ?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);

            resultSet=statement.executeQuery();
            // 由于此处是按照 blogId 来查询, blogId 又是主键.
            // 查询到的结果要么是 1 条记录, 要么是 0 条记录. 不会有别的情况.
            // 因此这里就没必要循环了, 直接条件判定即可.
            if(resultSet.next()){//查询到了，则进入；否则不会进来
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    //指定博客进行删除
    public void delete(int blogId){
        Connection connection=null;
        PreparedStatement statement=null;

        try {
            connection=DBUtil.getConnection();
            String sql="delete from blog where blogId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            statement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection, statement,null );
        }
    }

}
